<?php
use Admin\Admin_Object;
class ArtController extends Yaf_Controller_Abstract
{

    public function indexAction()
    {
        return $this->listAction();
    }

    public function addAction($artId = 0)
    {
        if (!Admin_Object::isAdmin()) {
            echo Common_Request::response(-2000, "请通过正确渠道提交");
            return FALSE;
        }

        $submit = $this->getRequest()->getQuery("submit", "0");
        if ($submit != "1") {
            echo Common_Request::response(-2001, "请通过正确渠道提交");
            return FALSE;
        }

        // 获取参数
        $title = $this->getRequest()->getPost("title", false);
        $contents = $this->getRequest()->getPost("contents", false);
        $author = $this->getRequest()->getPost("author", false);
        $cate = $this->getRequest()->getPost("cate", false);

        if (!$title || !$contents || !$author || !$cate) {
            echo Common_Request::response(-2002, "标题、内容、作者、分类信息为空，不能为空");
            return FALSE;
        }

        // 调用Model，做登录验证
        $model = new ArtModel();
        if ($lastId = $model->add(trim($title), trim($contents), trim($author), trim($cate), $artId)) {
            echo Common_Request::response(0, "", array("lastId" => $lastId));
        } else {
            echo Common_Request::response($model->errno, $model->errmsg);
        }
        return FALSE;
    }

    public function editAction()
    {
        if (!Admin_Object::isAdmin()) {
            echo Common_Request::response(-2000, '需要管理员权限才可以操作');
            return FALSE;
        }

        $artId = $this->getRequest()->getQuery("artId", "0");
        if (is_numeric($artId) && $artId) {
            return $this->addAction($artId);
        } else {
            echo Common_Request::response(-2003, '缺少必要的文章ID参数');
        }
        return TRUE;
    }

    public function delAction()
    {
        if (!Admin_Object::isAdmin()) {
            echo Common_Request::response(-2000, '需要管理员权限才可以操作');
            return FALSE;
        }

        $artId = $this->getRequest()->getQuery("artId", "0");
        if (is_numeric($artId) && $artId) {
            $model = new ArtModel();
            if ($model->del($artId)) {
                echo Common_Request::response(0, "");
            } else {
                echo Common_Request::response($model->errno, $model->errmsg);
            }
        } else {
            echo Common_Request::response(-2003, "缺少必要的文章标题参数");
        }
        return TRUE;
    }

    public function statusAction()
    {
        if (!Admin_Object::isAdmin()) {
            echo Common_Request::response(-2000, '需要管理员权限才可以操作');
            return FALSE;
        }

        $artId = $this->getRequest()->getQuery("artId", "0");
        $status = $this->getRequest()->getQuery("status", "offline");

        if (is_numeric($artId) && $artId) {
            $model = new ArtModel();
            if ($model->status($artId, $status)) {
                echo Common_Request::response(0, "");
            } else {
                echo Common_Request::response($model->errno, $model->errmsg);
            }
        } else {
            echo Common_Request::response(-2003, '缺少必要的文章标题参数');
        }
        return TRUE;
    }

    public function getAction()
    {
        $artId = $this->getRequest()->getQuery("artId", "0");

        if (is_numeric($artId) && $artId) {
            $model = new ArtModel();
            if ($data = $model->get($artId)) {
                echo Common_Request::response(0,"",$data);
            } else {
                echo Common_Request::response(-2009,"获取信息失败");
            }
        } else {
            echo Common_Request::response(-2003,"缺少必要的文章标题参数");
        }
        return TRUE;
    }

    public function listAction()
    {
        $pageNo = $this->getRequest()->getQuery("pageNo", "0");
        $pageSize = $this->getRequest()->getQuery("pageSize", "10");
        $cate = $this->getRequest()->getQuery("cate", "0");
        $status = $this->getRequest()->getQuery("status", "online");

        $model = new ArtModel();
        if ($data = $model->list($pageNo, $pageSize, $cate, $status)) {
            echo Common_Request::response(0,"",$data);
        } else {
            echo Common_Request::response(-2012,"获取文章列表失败");
        }
        return TRUE;
    }
}
